//
//  AXHomeNtsqVC.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/10/22.
//  Copyright © 2018年 tzhg. All rights reserved.
//

#import "AXHomeNtsqVC.h"
#import "LKCircleScrollView.h"
#import "Header.h"
#import "ZXCategories.h"
#import "UIView+SDAutoLayout.h"
#import "AXHomeNtsqFirstCell.h"
#import "AXHomeNtsqSecondCell.h"
#import "AXHomeZxktSecondModel.h"
#import "UIView+SDAutoLayout.h"
#import "ZXCategories.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "AXHomeZjzdVC.h"
#import "AXNtsqAxyxVC.h"
#import "AXMySrdzWdzwVC.h"
#import "LLSearchViewController.h"
//#import "WHRCropClassifyVC.h"
#import "CollectionViewController.h"


@interface AXHomeNtsqVC ()<UITableViewDelegate,UITableViewDataSource,LKCircleScrollViewDelegate, LKCircleScrollViewDataSource,UISearchBarDelegate>
/** headView */
@property (nonatomic ,strong) UIView *headView;
@property (nonatomic, strong) UITableView *tableView;
/** 广告图片 */
@property (nonatomic ,strong) UIImageView *advImageView;
/** 循环滚动View */
@property (nonatomic, strong) LKCircleScrollView *circleScrollView;
@property (nonatomic, strong) NSArray<NSString *> *scrollViewImageNames;
@property (nonatomic, strong) NSMutableArray<AXHomeZxktSecondModel *>  *modelsArray;
/** 假数据 */
@property (nonatomic, strong) NSArray<NSString *> *imageNames;
@property (nonatomic, strong) NSArray<NSString *> *intros;
@property (nonatomic, strong) NSArray<NSString *> *chName;
@property (nonatomic, strong) NSArray<NSString *> *enName;
@property (nonatomic, strong) NSArray<NSString *> *content;

@end

@implementation AXHomeNtsqVC

- (void)viewDidLoad {
    [super viewDidLoad];
    /** 假数据 */
    [self initData];
    // Do any additional setup after loading the view.
    [self creatView];
    [self setlayout];
    [self setupNavigationItems];
    self.tableView.tableHeaderView = _headView;
    [_headView setNeedsLayout];
    [_headView layoutIfNeeded];//刷新布局
    CGFloat height = [_headView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    _headView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, height);//headView 的大小
    _headView.backgroundColor = [UIColor redColor];
    self.tableView.tableHeaderView = _headView;
    
}

- (void)setupNavigationItems {
    
    /** 导航条左边的按钮 */
    //返回按钮
    UIButton *kfBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [kfBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    kfBtn.frame = CGRectMake(0, 0, 30, 30);
    [kfBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:kfBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIButton *searchButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-140, 30)];
    searchButton.backgroundColor = [UIColor whiteColor];
    searchButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [searchButton addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
    [searchButton setTitleColor:COLOR_Gray forState:UIControlStateNormal];
    [searchButton setTitle:@"农资分类" forState:UIControlStateNormal];
    searchButton.cornerRadius = 15;
    [searchButton setImage:[UIImage imageNamed:@"ss"] forState:UIControlStateNormal];
    self.navigationItem.titleView = searchButton;
    
    
    //分类按钮
    //返回按钮
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [rightBtn setTitleColor:COLOR_GREEN forState:UIControlStateNormal];
    [rightBtn setTitle:@"分类" forState:UIControlStateNormal];
    rightBtn.frame = CGRectMake(0, 0, 30, 30);
    [rightBtn addTarget:self action:@selector(rightBtn:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    
}


#pragma mark - Action

/** 分类按钮 */
- (void)rightBtn:(UIBarButtonItem *)btn {
    
    CollectionViewController *vc = [[CollectionViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
    
}

/** 搜索 */
- (void)search:(UIButton *)btu {
    
    LLSearchViewController *vc = [[LLSearchViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - Action

- (void)backAction:(UIBarButtonItem *)item {
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)initData {
    
     self.imageNames = @[@"zxkt"];
     self.intros = @[@"简介"];
    self.chName = @[@"中文名 : 苹果"];
    self.enName = @[@"英文名 : pingguo"];
     self.content = @[@"苹果（学名：Apple）是水果的一种，是蔷薇科苹果亚科苹果属植物，其树为落叶乔木。苹果的果实富含矿物质和维生素，是人们经常苹果（学名：Apple）是水果的一种，是蔷薇科苹果亚科苹果属植物，其树为落叶乔木。苹果的果实富含矿物质和维生素，是人们经常苹果（学名：Apple）是水果的一种，是蔷薇科苹果亚科苹果属植物，其树为落叶乔木。苹果的果实富含矿物质和维生素，是人们经常苹果（学名：Apple）是水果的一种，是蔷薇科苹果亚科苹果属植物，其树为落叶乔木。苹果的果实富含矿物质和维生素，是人们经常苹果（学名：Apple）是水果的一种，是蔷薇科苹果亚科苹果属植物，其树为落叶乔木。苹果的果实富含矿物质和维生素，是人们经常"];
}

- (void)creatView {
    
    _headView = [[UIView alloc]init];
    
    //添加scrollview
    self.scrollViewImageNames = @[@"shcm", @"sg2.jpeg.jpeg", @"sg3.jpeg.jpeg"];
    //self.circleScrollView = [[LKCircleScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
    self.circleScrollView.isAutoScroll = NO;
    self.circleScrollView = [[LKCircleScrollView alloc]init];
    self.circleScrollView.isAutoScroll = NO;
    self.circleScrollView.delegate = self;
    self.circleScrollView.dataSource = self;
    [self.headView addSubview:self.circleScrollView];
    

    self.tableView = [[UITableView alloc]init];
    /** 去掉cell线 */
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    //注册cell
    [self.tableView registerClass:[AXHomeNtsqSecondCell class] forCellReuseIdentifier:[AXHomeNtsqSecondCell reuseID]];
    [self.tableView registerClass:[AXHomeNtsqFirstCell class] forCellReuseIdentifier:[AXHomeNtsqFirstCell reuseID]];
    
    _headView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 129);
    self.tableView.tableHeaderView = self.headView;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
}

- (void)setlayout {
    
    if (_isHeight) {
        self.tableView.sd_layout
        .leftSpaceToView(self.view, 0)
        .topSpaceToView(self.view, 0)
        .widthIs(Wi)
        .heightIs(SCREEN_HEIGHT);
    } else {
        self.tableView.sd_layout
        .leftSpaceToView(self.view, 0)
        .topSpaceToView(self.view, 0)
        .widthIs(Wi)
        .heightIs(MyTableViewHeight);
    }

    
    self.circleScrollView.sd_layout
    .leftSpaceToView(self.headView,0)
    .rightSpaceToView(self.headView,0)
    .topSpaceToView(self.headView,0)
    .heightIs(129);
    
    [_headView setupAutoHeightWithBottomView:self.circleScrollView bottomMargin:0];
    
}


#pragma mark - tableViewDelegate & tableViewDatasorce
/** cell 的点击方法 */


//几个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return  1;
}

/** cell的高度 */

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    if (indexPath.section == 1) {
        
//        CGFloat f = [self.tableView cellHeightForIndexPath:indexPath model:self.modelsArray[indexPath.row] keyPath:@"model" cellClass:[AXHomeNtsqSecondCell class] contentViewWidth:SCREEN_WIDTH];
//
//        return f;
        
    return ((SCREEN_WIDTH-150*(4/3))+ 310);
        
    }


 
     return 100;
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //技术咨询、在线了解、安心U先、我的作物
    if (indexPath.section == 0) {
        
        AXHomeNtsqFirstCell *cell1 = [tableView dequeueReusableCellWithIdentifier:[AXHomeNtsqFirstCell reuseID] forIndexPath:indexPath];
        [cell1 setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        cell1.tapButtonClickedBlock = ^(NSInteger index) {
            //技术咨询
            if (index == 0) {
                
                AXHomeZjzdVC *vc = [[AXHomeZjzdVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
             
                
            }
        
            //安心U先
            else if (index == 1){
                

                 AXNtsqAxyxVC *vc = [[ AXNtsqAxyxVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
                
            }
            //我的作物
            else if (index == 2){
                
                  AXMySrdzWdzwVC *vc = [[AXMySrdzWdzwVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
       
                
            }
        };
        
        return cell1;
    }
    
    AXHomeNtsqSecondCell *cell2 = [tableView dequeueReusableCellWithIdentifier:[AXHomeNtsqSecondCell reuseID] forIndexPath:indexPath];
    [cell2 setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    cell2.img.image = [UIImage imageNamed:self.imageNames[indexPath.row]];
    cell2.introduceLabel.text = self.intros[indexPath.row];
    cell2.chineseNameLabel.text = self.chName[indexPath.row];
    cell2.englishNameLabel.text = self.enName[indexPath.row];
    cell2.contentLabel.text = self.content[indexPath.row];
    
    return cell2;
    
    
}


#pragma mark - Circle Scroll View Data Source & Delegate

- (NSInteger)numberOfViewsInCircleScrollView:(LKCircleScrollView *)circleScrollView {
    
    return self.scrollViewImageNames.count;
}

/** 视图来源 */
- (UIView *)circleScrollView:(LKCircleScrollView *)circleScrollView viewAtIndex:(NSInteger)index {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.scrollViewImageNames[index]]];
    imageView.contentMode =UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds=YES;//
    
    if (index == 0) {
        
        imageView.userInteractionEnabled = YES;
        
        
        
    }
    
    
    return imageView;
}

/** 点击View时调用  */
- (void)circleScrollView:(LKCircleScrollView *)circleScrollView didTapViewAtIndex:(NSInteger)index {
    if (index == 0) {
        //事件不会触发，点击事件可能被ScrollView截获
        //        ZXVoteProductionListViewController *vc = [[ZXVoteProductionListViewController alloc] init];
        //        [self.navigationController pushViewController:vc animated:YES];
    }
}





- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
}


#pragma mark - UISearchBar delegate
//将要开始编辑时的回调，返回为NO，则不能编辑
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    
    return NO;
}


@end



